/*
 * PagerTestAction.java
 *
 * Copyright 2012 xwwx, Inc. All rights reserved.
 * 
 */
package com.xwwx.action;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.xwwx.common.Constant;
import com.xwwx.common.PageList;
import com.xwwx.common.Pages;
import com.xwwx.service.UserService;

/*
* @author  xwwx
* @version 1.0, 2012-11-23
* @since   JDK1.6
* 
* 分页测试
*/
public class PageTestAction extends ActionSupport{

	private UserService userService;	//用户接口
	private PageList pageList;			//页对象
	private String pageno;				//页号
	private String pagination;			//导航条
	private String username;			//条件
	
	private static final Log logger = LogFactory.getLog(PageTestAction.class);
	
	public String execute(){

		Pages pages = new Pages();
		pages.setPerPageNum(Constant.ADMIN_DEFAULT_PAGE_SIZE);
		if(pageno == null){
			pageno = "1";
		}
		pages.setPage(Integer.parseInt(pageno));
		//pages.setWebpath(ServletActionContext.getRequest().getContextPath());
		if(username != null && username.length()>0){
			pages.setWherestr("&username=" + username);
			pageList = userService.getUserList("userid", "desc", pages, "username like '%" + username + "%'");			
		}else{
			pages.setWherestr("");
			pageList = userService.getUserList("userid", "desc", pages, "1=1");
		}
		
		/*
		StringBuffer sb = new StringBuffer();
		//"<a href="/news?page=2" class="prev_page" rel="prev">&laquo; 上一页</a> <a href="/news?page=1" rel="start">1</a> <a href="/news?page=2" rel="prev">2</a> <span class="current">3</span> <a href="/news?page=4" rel="next">4</a> <span class="gap">&hellip;</span> <a href="/news?page=539">539</a> <a href="/news?page=540">540</a> <a href="/news?page=4" class="next_page" rel="next">下一页 &raquo;</a>
		int cPage =	pages.getCpage(); 	 	//当前页
		int allPage = pages.getAllPage();   //总页数
		String path = ServletActionContext.getRequest().getContextPath();

		//当前页大于1
		if(cPage>1){
			sb.append("<a href=\""+path+"/page.do?pageno="+(cPage-1)+"\" class=\"prev_page\" rel=\"prev\">&laquo; 上一页</a>");
		}else{
			sb.append("<SPAN class=current>&laquo; 上一页</SPAN>");
		}
		
		int num1 = 6;   //第一临界点
		int num2 = 10;  //第二临界点
		int num3 = 5;	//第三临界点
		if(allPage <= num2){
			//全部显示
			for(int i = 1;i<=allPage;i++){
				if(cPage == i){
					sb.append("<SPAN class=current>"+i+"</SPAN>");
				}else{
					sb.append("<a href=\""+path+"/page.do?pageno="+i+"\" rel=\"prev\">"+i+"</a>");
				}
			}
		}else{
			//显示三段页
			if(cPage < num1){
				//点头几页
				for(int i = 1;i<=num1;i++){
					if(cPage == i){
						sb.append("<SPAN class=current>"+i+"</SPAN>");
					}else{
						sb.append("<a href=\""+path+"/page.do?pageno="+i+"\" rel=\"prev\">"+i+"</a>");
					}
				}
				sb.append("<span class=\"gap\">&hellip;</span>");
				for(int i = allPage-1;i<=allPage;i++){
					sb.append("<a href=\""+path+"/page.do?pageno="+i+"\" rel=\"prev\">"+i+"</a>");
				}
			}
			
			
			if(cPage>=num1 && (allPage-cPage > num3-1)){
				//点中间几页
				for(int i = 1;i<=2;i++){
					sb.append("<a href=\""+path+"/page.do?pageno="+i+"\" rel=\"prev\">"+i+"</a>");
				}	
				sb.append("<span class=\"gap\">&hellip;</span>");
				for(int i = cPage-1;i<cPage+2;i++){
					if(cPage == i){
						sb.append("<SPAN class=current>"+i+"</SPAN>");
					}else{
						sb.append("<a href=\""+path+"/page.do?pageno="+i+"\" rel=\"prev\">"+i+"</a>");
					}					
				}
				sb.append("<span class=\"gap\">&hellip;</span>");
				for(int i = allPage-1;i<=allPage;i++){
					sb.append("<a href=\""+path+"/page.do?pageno="+i+"\" rel=\"prev\">"+i+"</a>");
				}
			}

			if(allPage-cPage <= num3-1){
				//点最后几页
				for(int i = 1;i<=2;i++){
					sb.append("<a href=\""+path+"/page.do?pageno="+i+"\" rel=\"prev\">"+i+"</a>");
				}
				sb.append("<span class=\"gap\">&hellip;</span>");
				for(int i = allPage-num3;i<=allPage;i++){
					if(cPage == i){
						sb.append("<SPAN class=current>"+i+"</SPAN>");
					}else{
						sb.append("<a href=\""+path+"/page.do?pageno="+i+"\" rel=\"prev\">"+i+"</a>");
					}
				}
			}
		}
		
		//当前页大于总页数
		if(cPage == allPage){
			sb.append("<SPAN class=current>&laquo; 下一页</SPAN>");
		}else{
			sb.append("<a href=\""+path+"/page.do?pageno="+(cPage+1)+"\" class=\"next_page\" rel=\"next\">&laquo; 下一页</a>");
		}		
		pagination = sb.toString();
		*/
		pagination = pages.getUrlstr();
		return "list";
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public PageList getPageList() {
		return pageList;
	}

	public void setPageList(PageList pageList) {
		this.pageList = pageList;
	}

	public String getPageno() {
		return pageno;
	}

	public void setPageno(String pageno) {
		this.pageno = pageno;
	}

	public String getPagination() {
		return pagination;
	}

	public void setPagination(String pagination) {
		this.pagination = pagination;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

}
